﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Main_AjaUtils_ChatWindowBox : System.Web.UI.Page
{
    /*page defaults*/
    OfficeOnlineUser currentUser;
    
    private void AuthenticateUser()
    {
        try
        {
            currentUser = (OfficeOnlineUser)Session["CurrentUser"];
        }
        catch (Exception ex)
        {
            //Response.Redirect("../../login.aspx?urlReferrer=" + Request.Url);
        }

        if (currentUser == null)
        {
            //Response.Redirect("../../login.aspx?urlReferrer=" + Request.Url);
        }
    }
    /*end page defaults*/

    protected void Page_Load(object sender, EventArgs e)
    {
        AuthenticateUser();
        currentUserID.Value = currentUser.UserName;
        
        String action = Request.QueryString["action"];
        if (action == "sendmsg")
        {
            int result = SendMessage();
            switch (result)
            {
                case 1:
                    frmChatWindowBox.InnerHtml = "<response>Success</response>";
                    
                    ChatRoom room = new ChatRoom();
                    room.ID = Request.QueryString["roomID"];
                    String senderID = Request.QueryString["senderID"];
                    
                    String otherUserID = room.GetOtherUserInRoom(senderID);

                    // Forcibly make the recipient join the room so that he would see your messages
                    room.Join(senderID);
                    room.Join(otherUserID);
                    break;
                default:
                    frmChatWindowBox.InnerHtml = "<response>Fail</response>";
                    break;
            }
        }
        if (action == "leaveroom")
        {
            int result = LeaveRoom();
            switch (result)
            {
                case 1:
                    ChatRoom room = new ChatRoom();
                    room.ID = Request.QueryString["roomID"];
                    if (!room.StillHasMembers())
                    {
                        int result2 = room.Close();
                        if(result2 == 1)
                            frmChatWindowBox.InnerHtml = "<response>Success</response>";
                    }
                    else
                        frmChatWindowBox.InnerHtml = "<response>Success</response>";                    
                    break;
                default:
                    frmChatWindowBox.InnerHtml = "<response>Fail</response>";
                    break;
            }
        }
    }

    public int SendMessage()
    {
        ChatRoom room = new ChatRoom();
        ChatMessage msg = new ChatMessage();
        msg.RoomID = Request.QueryString["roomID"];
        msg.SenderID = Request.QueryString["senderID"];
        msg.Text = Request.QueryString["msg"];

        return room.SendMessage(msg);
    }

    public int LeaveRoom()
    {
        ChatRoom room = new ChatRoom();
        room.ID = Request.QueryString["roomID"];
        String userID = Request.QueryString["userID"];

        return room.Leave(userID);
    }
}
